package com.bsj.power.pls.handle;

import com.bsj.consumerQ.method.QueueAndRunT;
import com.bsj.power.common.netty.CTData;
import com.bsj.power.common.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 所有的数据都要经过这个queue，起着一个分流的作用，真正分流处理的是DataHandle
 *
 * @author wengxiang
 * @date 2021-07-20 9:42
 **/
@Slf4j
public class CTDataQueue {

    private static QueueAndRunT<CTData> dtoQueue;

    private static CTDataHandle ctDataHandle;

    public static void start() {
        if (ctDataHandle == null) {
            ctDataHandle = SpringUtil.getBean(CTDataHandle.class);
        }
        dtoQueue = new QueueAndRunT<>();
        dtoQueue.start("ctDataQueue", list -> {
            for (CTData ctData : list) {
                log.info("测试打印日志，接收到数据={}", ctData.toString());
                ctDataHandle.handleData(ctData);
            }
        });
    }

    public static void stop() {
        if (dtoQueue != null) {
            dtoQueue.stop();
        }
    }

    public static void push(CTData ctData) throws Exception {
        if (ctData != null) {
            dtoQueue.push(ctData);
        }
    }
}
